package com.xfzp.d3;

/**
 * 坐标。
 * 
 * @author AndroidApe
 * @version 2011-11-9
 */
public class Coord {
	private float x;// x轴坐标
	private float y;// y轴坐标
	private float z;// z轴坐标

	public Coord() {
	}

	public Coord(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	/**
	 * 重置坐标
	 * 
	 * @param x
	 * @param y
	 * @param z
	 */
	public void reset(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	/**
	 * 乘以一个4 x 4的矩阵
	 * 
	 * @param m
	 */
	public void multiply(float[] m) {
		float x0 = x * m[0] + y * m[4] + z * m[8] + m[12];
		float y0 = x * m[1] + y * m[5] + z * m[9] + m[13];
		float z0 = x * m[2] + y * m[6] + z * m[10] + m[14];
		x = x0;
		y = y0;
		z = z0;
	}

	/**
	 * 设置指定轴的坐标值
	 * 
	 * @param axis
	 * @param v
	 */
	public void set(byte axis, float v) {
		if (axis == Axis.X)
			x(v);
		else if (axis == Axis.Y)
			y(v);
		else if (axis == Axis.Z)
			z(v);
		else
			throw new IllegalArgumentException("axis的可选值为Axis.X、Axis.Y、Axis.Z");
	}

	/**
	 * 返回指定轴的坐标值
	 * 
	 * @param axis
	 * @return
	 */
	public float get(byte axis) {
		if (axis == Axis.X)
			return x();
		else if (axis == Axis.Y)
			return y();
		else if (axis == Axis.Z)
			return z();
		else
			throw new IllegalArgumentException("axis的可选值为Axis.X、Axis.Y、Axis.Z");
	}

	/**
	 * 返回x坐标
	 * 
	 * @return
	 */
	public float x() {
		return x;
	}

	/**
	 * 返回y坐标
	 * 
	 * @return
	 */
	public float y() {
		return y;
	}

	/**
	 * 返回z坐标
	 * 
	 * @return
	 */
	public float z() {
		return z;
	}

	/**
	 * 设置x坐标值
	 * 
	 * @param x
	 */
	public void x(float x) {
		this.x = x;
	}

	/**
	 * 设置y坐标值
	 * 
	 * @param y
	 */
	public void y(float y) {
		this.y = y;
	}

	/**
	 * 设置z坐标值
	 * 
	 * @param z
	 */
	public void z(float z) {
		this.z = z;
	}

	/**
	 * 坐标相减
	 * 
	 * @param s
	 *            减数坐标
	 * @return
	 */
	public Coord subtract(Coord c) {
		return new Coord(this.x() - c.x(), this.y() - c.y(), this.z() - c.z());
	}

	/**
	 * 坐标相加
	 * 
	 * @param c
	 * @return
	 */
	public Coord addition(Coord c) {
		return new Coord(this.x() + c.x(), this.y() + c.y(), this.z() + c.z());
	}

	/**
	 * 标量相乘
	 * 
	 * @param s
	 * @return
	 */
	public Coord scalar(float s) {
		return new Coord(this.x() * s, this.y() * s, this.z() * s);
	}

	/**
	 * 交叉相乘
	 * 
	 * @param c
	 * @return
	 */
	public Coord cross(Coord c) {
		return new Coord(this.y() * c.z() - this.z() * c.y(), this.z() * c.x()
				- this.x() * c.z(), this.x() * c.y() - this.y() * c.x());
	}

	/**
	 * 点乘
	 * 
	 * @param c
	 * @return
	 */
	public float dot(Coord c) {
		return this.x() * c.x() + this.y() * c.y() + this.z() * c.z();
	}
}
